import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class QMessageBoxDemo(QWidget):

    def __init__(self):
        super(QMessageBoxDemo, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('消息案例')
        self.resize(300,300)

        Layout = QVBoxLayout()

        self.btn1 = QPushButton(self)
        self.btn1.setText('显示关于对话框')
        self.btn1.clicked.connect(self.showDialog)

        self.btn2 = QPushButton(self)
        self.btn2.setText('消息对话框')
        self.btn2.clicked.connect(self.showDialog)

        self.btn3 = QPushButton(self)
        self.btn3.setText('警告对话框')
        self.btn3.clicked.connect(self.showDialog)

        self.btn4 = QPushButton(self)
        self.btn4.setText('提问对话框')
        self.btn4.clicked.connect(self.showDialog)

        self.btn5 = QPushButton(self)
        self.btn5.setText('错误对话框')
        self.btn5.clicked.connect(self.showDialog)

        Layout.addWidget(self.btn1)
        Layout.addWidget(self.btn2)
        Layout.addWidget(self.btn3)
        Layout.addWidget(self.btn4)
        Layout.addWidget(self.btn5)

        self.setLayout(Layout)

    def showDialog(self):

        text = self.sender().text()
        if text == '显示关于对话框':
            QMessageBox.about(self, '关于', '这是一个对话框')
        elif text == '消息对话框':
            reply = QMessageBox.information(self, '消息', '这是一个对话框',QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
            # QMessageBox.about(self, '关于', '这是一个对话框'/)
            print(reply == QMessageBox.Yes)
        elif text == '警告对话框':
            QMessageBox.warning(self, '警告', '这是一个警告框', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        elif text == '提问对话框':
            QMessageBox.question(self, '提问', '这是一个提问对话框', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        elif text == '错误对话框':
            QMessageBox.critical(self, '错误', '这是一个错误对话框', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = QMessageBoxDemo()
    ex.show()
    sys.exit(app.exec_())